升級指南
0.13.x-> 0.14.0
TypeScript定義
TypeScript軸定義已更新,以匹配API軸並使用ES2015模塊語法。
請使用以下import
語句在TypeScript中導入軸:
import axios from 'axios';
axios.get('/foo')
.then(response => console.log(response))
.catch(error => console.log(error));
agent
配置選項
該agent
配置選項已被替換為兩個新的選項:httpAgent
和httpsAgent
。請改用它們。
{
// Define a custom agent for HTTP
httpAgent: new http.Agent({ keepAlive: true }),
// Define a custom agent for HTTPS
httpsAgent: new https.Agent({ keepAlive: true })
}
progress
配置選項
該progress
配置選項已被替換的onUploadProgress
和onDownloadProgress
選項。
{
// Define a handler for upload progress events
onUploadProgress: function (progressEvent) {
// ...
},
// Define a handler for download progress events
onDownloadProgress: function (progressEvent) {
// ...
}
}
0.12.x-> 0.13.0
該0.13.0
版本包含對自定義適配器和錯誤處理的一些更改。
錯誤處理
在此版本之前,錯誤可能是服務器響應中包含錯誤的狀態代碼或實際錯誤Error
。在此版本中,Promise始終會拒絕帶有Error
。如果收到響應,Error
則還將包括響應。
axios.get('/user/12345')
.catch((error) => {
console.log(error.message);
console.log(error.code); // Not always specified
console.log(error.config); // The config that was used to make the request
console.log(error.response); // Only available if response was received from the server
});
要求適配器
- 現在,將響應轉換器稱為適配器的外部。
- 請求適配器返回
Promise
。
這意味著您不再需要調用transformData
響應數據。您也不再接收resolve
並reject
在適配器中的參數。
先前的代碼:
function myAdapter(resolve, reject, config) {
var response = {
data: transformData(
responseData,
responseHeaders,
config.transformResponse
),
status: request.status,
statusText: request.statusText,
headers: responseHeaders
};
settle(resolve, reject, response);
}
function myAdapter(config) {
return new Promise(function (resolve, reject) {
var response = {
data: responseData,
status: request.status,
statusText: request.statusText,
headers: responseHeaders
};
settle(resolve, reject, response);
});
}
有關更多詳細信息,請參見相關的提交:
0.5.x-> 0.6.0
該0.6.0
版本主要包含一些錯誤修復,但是升級時需要注意幾件事。
ES6 Promise Polyfill
Up until the 0.6.0
release ES6 Promise
was being polyfilled using es6-promise. With this release, the polyfill has been removed, and you will need to supply it yourself if your environment needs it.
require('es6-promise').polyfill();
var axios = require('axios');
This will polyfill the global environment, and only needs to be done once.
axios.success
/axios.error
The success
, and error
aliases were deprectated in 0.4.0. As of this release they have been removed entirely. Instead please use axios.then
, and axios.catch
respectively.
axios.get('some/url')
.then(function (res) {
/* ... */
})
.catch(function (err) {
/* ... */
});
UMD
Previous versions of axios shipped with an AMD, CommonJS, and Global build. This has all been rolled into a single UMD build.
// AMD
require(['bower_components/axios/dist/axios'], function (axios) {
/* ... */
});
// CommonJS
var axios = require('axios/dist/axios');